#include "stdafx.h"
#include "LeoGraphics.h"
#include "UserInterface.h"


bool UserInterface::texture_loaded;
int UserInterface::ScrollingBarVertical_;

LeoGraphics::LeoGraphics()
{
	//Initialize variables
	canDraw=false;
	canRender=false;
	viewSlices=false;
	numberOfTriangles=0;
	triangles=NULL;
	cellH=0;
	cellW=0;
	cellF=0;
	autoRot=false;

	// EDO
	RotationDirection=3;

	rtri=0.0f;				
	ptri=0.0f;
	px = 0;
	py = 0;
	vertices=NULL;
	verts = NULL;
	normals = NULL;
	indices = NULL;
	textCoords = NULL;

	quadVerts = NULL;
	quadColors = NULL;
	zoomFactor=1.0;
	//lighting
	lightChange=false;
	lightSelect=0;
	ambient_color = new float[4]; 


	diff_color = new float[4];


	spec_color = new float[4];

	texture1 = new GLuint[1];
	texture2 = new GLuint[1];

	ReconstructionAlgorithm = MarchingCubesAlgorithm;
	
	//MessageBox(NULL,L"Start to read file, this will take a while...",L"Info",MB_OK|MB_ICONEXCLAMATION);
	
	//MessageBox(NULL,L"Finished, please click OK",L"Info",MB_OK|MB_ICONEXCLAMATION);

}

int LeoGraphics::changeAlgorithm(int i, HWND hwndParent, HINSTANCE g_hinst)
{
	if(vertices==NULL)
	{
		MessageBox(NULL,L"Please load RAW data first!",L"Info",MB_OK|MB_ICONEXCLAMATION);
		return 1;
	}
	else
	{
		canRender=true;
		if(i==0)
		{
			ReconstructionAlgorithm = MarchingFacesAlgorithm;
			//ReconstructionAlgorithm = MarchingTetrahedronAlgorithm;
			LoadTrianglesToPointer(hwndParent, g_hinst);
		}
		else if(i==1)
		{
			ReconstructionAlgorithm = MarchingCubesAlgorithm;
			LoadTrianglesToPointer(hwndParent, g_hinst);
		}
	}
	return 1;
}
int LeoGraphics::LoadGLTextures()  
{
	
	texture1[0] = SOIL_load_OGL_texture
		(
		"Pics/photo.png",
		SOIL_LOAD_AUTO,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_INVERT_Y
		);
	texture2[0] = SOIL_load_OGL_texture
		(
		"Pics/UI.png",
		SOIL_LOAD_AUTO,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_INVERT_Y
		);

	if(texture1[0] == 0||texture2[0] == 0)
		return false;


	// Typical Texture Generation Using Data From The Bitmap
	

	return true;	
}
int LeoGraphics::CheckInside(LPARAM lParam)
{
	float x=(float)(LOWORD(lParam));
	float y=(float)(HIWORD(lParam));
	if (x>25&&x<672&&y>45&&y<630)
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}
int LeoGraphics::Read(string s, HWND hwnd, HINSTANCE hinst)
{
	MessageBox(NULL,L"Start to read file, this will take a while...",L"Info",MB_OK|MB_ICONEXCLAMATION);

	
	reader = new RAWReader();
	if (vertices==NULL)
	{
		//vertices = reader.ReadRawFile(s, cellH, cellW, cellF, 8, 19);
		vertices = reader->ReadRawFileWithProgress(hwnd, hinst, s, cellH, cellW, cellF, 8, 19);
	}
	else
	{
		delete []vertices;
		//vertices = reader.ReadRawFile(s, cellH, cellW, cellF, 8, 19);
		vertices = reader->ReadRawFileWithProgress(hwnd, hinst, s, cellH, cellW, cellF, 8, 19);
	}
	canDraw=false;
	canRender=true;
	viewSlices=false;
	MessageBox(NULL,L"Finished, please click OK",L"Info",MB_OK|MB_ICONEXCLAMATION);
	return TRUE;
}
void LeoGraphics::RotKeyDown(LPARAM lParam)
{
	px=(float)(LOWORD(lParam));
	py=(float)(HIWORD(lParam));
}

void LeoGraphics::ChangeZoom(float val)
{
	zoomFactor=val;
}

float LeoGraphics::CalAbVal(float x, float y, float z)
{
	return sqrt(x*x + y*y + z*z);
}

void LeoGraphics::MouseRotation(LPARAM lParam)
{
	//MessageBox(NULL,L"Initialization Failed.",L"ERROR",MB_OK|MB_ICONEXCLAMATION);
	//Mouse control for rotation, in order to obtain performance, atan2 calculation were removed. 
	//Although less accurate but more efficient.
	float cx=(float)(LOWORD(lParam));
	float cy=(float)(HIWORD(lParam));
	//if (!(cx>0&&cx<1024)||!(cy>0&&cy<1024))
	//{
	//	return;
	//}
	float yAngle=0.0;
	float xAngle=0.0;

	if ((cx<349&&px>349)||(cx>349&&px<349))
	{
		//yAngle=10*abs(atan2(cx-512,768*zoomFactor))+abs(atan2(px-512,768*zoomFactor));
		yAngle=0.05*abs(cx-px);
		if(cx<px) yAngle=-yAngle; 
	}
	else if ((cx>349&&px>349)||(cx<349&&px<349))
	{
		//yAngle=10*abs(abs(atan2(cx-512,768*zoomFactor))-abs(atan2(px-512,768*zoomFactor)));
		yAngle=0.05*abs(cx-px);
		if(cx<px) yAngle=-yAngle;
	}

	if ((cy<338&&py>338)||(cy>338&&py<338))
	{
		//xAngle=10*abs(atan2(cy-512,768*zoomFactor))+abs(atan2(py-512,768*zoomFactor));
		xAngle=0.05*abs(cy-py);
		if(py<cy) xAngle=-xAngle; 
	}
	else if ((cy>338&&py>338)||(cy<338&&py<338))
	{
		//xAngle=10*abs(abs(atan2(cy-512,768*zoomFactor))-abs(atan2(py-512,768*zoomFactor)));
		xAngle=0.05*abs(cy-py);
		if(py<cy) xAngle=-xAngle;
	}
	//int yDif = (HIWORD(lParam)-py)/256;

	//ptri+=yDif;		
	rtri+=yAngle;

	//EDO
	ptri-=xAngle;
}

GLvoid LeoGraphics::ReSizeGLScene(GLsizei width, GLsizei height)		// Resize And Initialize The GL Window
{
//	if (height==0)										// Prevent A Divide By Zero By
//	{
//		height=1;										// Making Height Equal One
//	}
//
//	if (loop_==0)
//	{
//		glViewport(200,200,400,400);						// Reset The Current Viewport
//
//		glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
//		glLoadIdentity();									// Reset The Projection Matrix
//
//		// Calculate The Aspect Ratio Of The Window
//		gluPerspective(60.0f,(GLfloat)width/(GLfloat)height,1.0f,2048);
//		//glOrtho(0, width, height, 0, 1, 1024);
//		//glFrustum(0,width,height,0,1,510);
//
//		glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
//		glLoadIdentity();									// Reset The Modelview Matrix
//		gluLookAt(128,128,768,0,0,0,0,1,0);
//	}
//	if (loop_==1)
//	{
//		glViewport(0,0,width,height);						// Reset The Current Viewport
//
//		glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
//		glLoadIdentity();									// Reset The Projection Matrix
//
//		// Calculate The Aspect Ratio Of The Window
//		glOrtho(0, width, height, 0, -1, 1024);
//		//glOrtho(0, width, height, 0, 1, 1024);
//		//glFrustum(0,width,height,0,1,510);
//
//		glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
//		glLoadIdentity();									// Reset The Modelview Matrix
//	}
//

}

int LeoGraphics::InitGL(GLvoid)										// All Setup For OpenGL Goes Here
{
	UserInterface::CreateVariableUserInterface();
	UserInterface::texture_loaded=false;
	UserInterface::ScrollingBarVertical_=285;
	glEnable(GL_SCISSOR_TEST); 
	glEnable(GL_TEXTURE_2D);
	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Black Background
	glClearDepth(1.0f);									// Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations


	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

	float light_color[] = {1, 1, 1, 1};
	float light_position[] = {0, 0, 368, 0};

	glLightfv(GL_LIGHT0, GL_COLOR, light_color);
	glLightfv(GL_LIGHT0, GL_POSITION, light_position);

	GLfloat whiteColor[] = {1.0, 1.0, 1.0, 1.0};

	glLightfv(GL_LIGHT1, GL_POSITION, light_position);
	glLightfv(GL_LIGHT1, GL_AMBIENT, whiteColor);
	glLightfv(GL_LIGHT1, GL_DIFFUSE, whiteColor);
	glLightfv(GL_LIGHT1, GL_SPECULAR, whiteColor); 

	glEnable(GL_LIGHTING);
	ambient_color[0]=1.0;
	ambient_color[1]=1.0;
	ambient_color[2]=1.0;
	ambient_color[3]=1.0;

	//diff_color = new float[4];
	diff_color[0]=0.887;
	diff_color[1]=0.844;
	diff_color[2]=0.844;
	diff_color[3]=1.0;

	//spec_color = new float[4];
	spec_color[0]=0.887;
	spec_color[1]=0.844;
	spec_color[2]=0.844;
	spec_color[3]=1.0;
	//GLfloat LightAmbient[]=	{ 0.5f, 0.5f, 0.5f, 1.0f };
	//GLfloat LightDiffuse[]=	{ 1.0f, 1.0f, 1.0f, 1.0f };
	//GLfloat LightPosition[]={ 0.0f, 0.0f, 512.0f, 1.0f };
	//glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);		// Setup The Ambient Light
	//glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);		// Setup The Diffuse Light
	//glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);	// Position The Light
	//glEnable(GL_LIGHT1);

	//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	//float light_color[] = {0.5, 0.5, 0.5, 1};
	//float light_position[] = {0, 0, 768, 1};
	//glLightfv(GL_LIGHT0, GL_COLOR, light_color);
	//glLightfv(GL_LIGHT0, GL_POSITION, light_position);
	//glEnable(GL_LIGHT0);
	//glEnable(GL_LIGHTING);

	////set original The Phong's Shading Model.
	//ambient_color = new float[4]; 
	//ambient_color[0]=0.5;
	//ambient_color[1]=0.5;
	//ambient_color[2]=0.5;
	//ambient_color[3]=1.0;

	//diff_color = new float[4];
	//diff_color[0]=0.887;
	//diff_color[1]=0.844;
	//diff_color[2]=0.844;
	//diff_color[3]=1.0;

	//spec_color = new float[4];
	//spec_color[0]=0.887;
	//spec_color[1]=0.844;
	//spec_color[2]=0.844;
	//spec_color[3]=1.0;
	//glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient_color);
	//glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diff_color);
	//glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec_color);
	//glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 128.0);

	//This lighting also can do.
	/*GLfloat afAmbientWhite [] = {0.25, 0.25, 0.25, 1.00}; 
	GLfloat afAmbientRed   [] = {0.25, 0.00, 0.00, 1.00}; 
	GLfloat afAmbientGreen [] = {0.00, 0.25, 0.00, 1.00}; 
	GLfloat afAmbientBlue  [] = {0.00, 0.00, 0.25, 1.00}; 
	GLfloat afDiffuseWhite [] = {0.75, 0.75, 0.75, 1.00}; 
	GLfloat afDiffuseRed   [] = {0.75, 0.00, 0.00, 1.00}; 
	GLfloat afDiffuseGreen [] = {0.00, 0.75, 0.00, 1.00}; 
	GLfloat afDiffuseBlue  [] = {0.00, 0.00, 0.75, 1.00}; 
	GLfloat afSpecularWhite[] = {1.00, 1.00, 1.00, 1.00}; 
	GLfloat afSpecularRed  [] = {1.00, 0.25, 0.25, 1.00}; 
	GLfloat afSpecularGreen[] = {0.25, 1.00, 0.25, 1.00}; 
	GLfloat afSpecularBlue [] = {0.25, 0.25, 1.00, 1.00}; 
	GLfloat afPropertiesAmbient [] = {0.50, 0.50, 0.50, 1.00}; 
	GLfloat afPropertiesDiffuse [] = {0.75, 0.75, 0.75, 1.00}; 
	GLfloat afPropertiesSpecular[] = {1.00, 1.00, 1.00, 1.00};
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

	glLightfv( GL_LIGHT0, GL_AMBIENT,  afPropertiesAmbient); 
	glLightfv( GL_LIGHT0, GL_DIFFUSE,  afPropertiesDiffuse); 
	glLightfv( GL_LIGHT0, GL_SPECULAR, afPropertiesSpecular); 
	glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, 1.0); 

	glEnable( GL_LIGHT0 ); 

	glMaterialfv(GL_BACK,  GL_AMBIENT,   afAmbientGreen); 
	glMaterialfv(GL_BACK,  GL_DIFFUSE,   afDiffuseGreen); 
	glMaterialfv(GL_FRONT, GL_AMBIENT,   afAmbientBlue); 
	glMaterialfv(GL_FRONT, GL_DIFFUSE,   afDiffuseBlue); 
	glMaterialfv(GL_FRONT, GL_SPECULAR,  afSpecularWhite); 
	glMaterialf( GL_FRONT, GL_SHININESS, 25.0); */
	//glColor4fv(ambient_color);
	//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	LoadGLTextures();
	return TRUE;										// Initialization Went OK
}

void LeoGraphics::LoadTrianglesToPointer(HWND hwndParent, HINSTANCE g_hinst)
{
	//triangles = MarchingCubesAlgorithm(cellH, cellW
	//	, cellF, vertices, 120, numberOfTriangles);
	if(!canRender)
	{
		MessageBox(NULL,L"Please load RAW data first!",L"Info",MB_OK|MB_ICONEXCLAMATION);
		return;
	}
	MessageBox(NULL,L"Algorithm will be started, this might take a while...",L"Info",MB_OK|MB_ICONEXCLAMATION);
	
	if (verts!=NULL)
	{
		delete []verts;
		verts=NULL;
	}
	if (normals!=NULL)
	{
		delete []normals;
		normals=NULL;
	}
	if (indices!=NULL)
	{
		delete []indices;
		indices=NULL;
	}
	if (textCoords!=NULL)
	{
		delete []textCoords;
		textCoords=NULL;
	}
	
	if (triangles!=NULL)
	{
		delete []triangles;
	}
	triangles = ReconstructionAlgorithm(cellH, cellW
		, cellF, vertices, 120, numberOfTriangles, hwndParent, g_hinst);

	if (verts==NULL)
	{
		verts = new float[numberOfTriangles*9];
	}
	if (normals==NULL)
	{
		normals = new float[numberOfTriangles*9];
	}
	if (indices==NULL)
	{
		indices = new GLuint[numberOfTriangles*9];
	}
	if (textCoords==NULL)
	{
		textCoords = new float[numberOfTriangles*6];
	}




	for (unsigned int i=0; i<numberOfTriangles; i++)
	{

		verts[i*9]=-(triangles[i].v[0].y-256);
		verts[i*9+1]=triangles[i].v[0].z-256;
		verts[i*9+2]=triangles[i].v[0].x-256;
		verts[i*9+3]=-(triangles[i].v[1].y-256);
		verts[i*9+4]=triangles[i].v[1].z-256;
		verts[i*9+5]=triangles[i].v[1].x-256; 
		verts[i*9+6]=-(triangles[i].v[2].y-256);
		verts[i*9+7]=triangles[i].v[2].z-256;
		verts[i*9+8]=triangles[i].v[2].x-256;

		float length0=CalAbVal(triangles[i].v[0].z-256+triangles[i].normal.z,triangles[i].v[0].x-256+triangles[i].normal.x,-(triangles[i].v[0].y-256)+triangles[i].normal.y);
		float length1=CalAbVal(triangles[i].v[1].z-256+triangles[i].normal.z,triangles[i].v[1].x-256+triangles[i].normal.x,-(triangles[i].v[1].y-256)+triangles[i].normal.y);
		float length2=CalAbVal(triangles[i].v[2].z-256+triangles[i].normal.z,triangles[i].v[2].x-256+triangles[i].normal.x,-(triangles[i].v[2].y-256)+triangles[i].normal.y);

		normals[i*9]=(-(triangles[i].v[0].y-256)+triangles[i].normal.y)/length0;
		normals[i*9+1]=(triangles[i].v[0].z-256+triangles[i].normal.z)/length0;
		normals[i*9+2]=(triangles[i].v[0].x-256+triangles[i].normal.x)/length0; 

		normals[i*9+3]=(-(triangles[i].v[1].y-256)+triangles[i].normal.y)/length1;	
		normals[i*9+4]=(triangles[i].v[1].z-256+triangles[i].normal.z)/length1;
		normals[i*9+5]=(triangles[i].v[1].x-256+triangles[i].normal.x)/length1; 

		normals[i*9+6]=(-(triangles[i].v[2].y-256)+triangles[i].normal.y)/length2;
		normals[i*9+7]=(triangles[i].v[2].z-256+triangles[i].normal.z)/length2;		
		normals[i*9+8]=(triangles[i].v[2].x-256+triangles[i].normal.x)/length2;

		indices[i*9]=i*9;
		indices[i*9+1]=i*9+1;
		indices[i*9+2]=i*9+2;
		indices[i*9+3]=i*9+3;
		indices[i*9+4]=i*9+4;
		indices[i*9+5]=i*9+5; 
		indices[i*9+6]=i*9+6;
		indices[i*9+7]=i*9+7;
		indices[i*9+8]=i*9+8;

		textCoords[i*6]=0.0;
		textCoords[i*6+1]=1.0;
		textCoords[i*6+2]=1.0;
		textCoords[i*6+3]=0.0;
		textCoords[i*6+4]=0.0;
		textCoords[i*6+5]=0.0;
	}
	canDraw=true;
	viewSlices=false;
	//canRender=false;
	zoomFactor=1.0;
	delete []triangles; //release memory.
	triangles=NULL;
	
}

int LeoGraphics::DrawGLScene(GLsizei width, GLsizei height)									// Here's Where We Do All The Drawing
{

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // EDO



	for(int loop=0; loop<3; loop++)
	{
		

		if (loop==1)
		{
			glEnable(GL_LIGHT1);
			glViewport(0,0,width,height);						// Reset The Current Viewport
			glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
			glLoadIdentity();									// Reset The Projection Matrix
			// Calculate The Aspect Ratio Of The Window
			glOrtho(0, width, height, 0, -1, 1024);
			//glOrtho(0, width, height, 0, 1, 1024);
			//glFrustum(0,width,height,0,1,510);
			
		}
		if (loop==0)
		{
			if((!canDraw)||viewSlices) continue;	//if 3D rendering is not prepared, continue.
			glViewport(25,138,647,585);						// Reset The Current Viewport
			glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
			glLoadIdentity();									// Reset The Projection Matrix
			// Calculate The Aspect Ratio Of The Window
			gluPerspective(80.0f,(GLfloat)622/(GLfloat)447,1.0f,2048);
			//glOrtho(0, 647, 585, 0, 1, 1024);
			//glFrustum(0,width,height,0,1,510);
			glEnable(GL_LIGHT0);
			//gluLookAt(128,128,368,0,0,0,0,1,0);

		}
		if (loop==2)
		{
			if(!viewSlices||canDraw) continue;
			glDisable(GL_LIGHTING);
			glViewport(25,138,647,585);						// Reset The Current Viewport
			glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
			glLoadIdentity();									// Reset The Projection Matrix

			glOrtho(0, 647, 585, 0, -1, 1);

		}
		// EDO: Removed something

		glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
		glLoadIdentity();									// Reset The Modelview Matrix
					
		if (loop==1)
		{
			// EDO: Background

			float red = 215.0f/255.0f;
			float green = 223.0f/255.0f;
			float blue = 234.0f/255.0f;
			float grey = 189.0f/255.0f;

			glScissor(0,0,25,height);
			glClearColor(red,green,blue,1.0);
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

			glScissor(25,0,width-25,138);
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

			glScissor(25,723,width-25,height-723);
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

			glScissor(672,138,700-672,585);
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

			glScissor(725,138,width-725,585);
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

			glScissor(700,138,25,585);
			glClearColor(grey,grey,grey,1.0);
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

			glScissor(0,0,width,height);
			glClearColor(0.0,0.0,0.0,0.0);





			glBindTexture(GL_TEXTURE_2D, texture2[0]);
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
			UserInterface::CreateUserInterface(texture2[0]);
			UserInterface::texture_loaded=true;
			glDisable(GL_LIGHT1);

		}
		if (loop==0)
		{
			glScissor(25,138,647,585);
			glClearColor(0.0,0.0,0.0,0.0);
			glBindTexture(GL_TEXTURE_2D, texture1[0]);
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

			
			glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient_color);
			glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diff_color);
			//glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec_color);
			glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80.0);
			//glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
			glTranslatef(0.0f,0.0f,-468.0f*zoomFactor);
			//glTranslatef(324.0f,293.0f,-468.0f*zoomFactor);
			//glRotatef(90,1.0f,0.0f,0.0f);

			// EDO
			if (autoRot)
			{
				if (RotationDirection==1)	rtri-=0.5;
				if (RotationDirection==2)	ptri+=0.5;
				if (RotationDirection==3)	rtri+=0.5;
				if (RotationDirection==4)	ptri-=0.5;
			}

			glRotatef(rtri,0.0f,1.0f,0.0f);	
			glRotatef(ptri,1.0f,0.0f,0.0f);	
			//gluLookAt(10,10,10,0,0,0,0,1,0);
			//GLfloat colors[]={0.5, 0.5, 0.5, 1.0}; 
			//glColor4fv(colors);
			//glBindTexture(GL_TEXTURE_2D, texture[0]);
			glEnableClientState(GL_VERTEX_ARRAY);
			glEnableClientState(GL_NORMAL_ARRAY);
			glEnableClientState(GL_TEXTURE_COORD_ARRAY);
			//glEnableClientState(GL_COLOR_ARRAY);
			//glColorPointer(3, GL_FLOAT, 0, colors);
			glVertexPointer(3, GL_FLOAT, 0, verts);
			glNormalPointer(GL_FLOAT, 0, normals);
			glTexCoordPointer(2, GL_FLOAT, 0, textCoords);
			glDrawElements(GL_TRIANGLES, numberOfTriangles*3, GL_UNSIGNED_INT, indices);
			glDisableClientState(GL_VERTEX_ARRAY);
			glDisableClientState(GL_NORMAL_ARRAY);
			glDisableClientState(GL_TEXTURE_COORD_ARRAY);
			glDisable(GL_LIGHT0);
										
		}
		if (loop==2)
		{
			curFrame=(int)((float)((UserInterface::ScrollingBarVertical_-7)*reader->F/572.0f));
			if (curFrame<=1) curFrame=1;
			if (curFrame>=reader->F) curFrame=reader->F;

			if(quadVerts!=NULL)
			{
				delete[] quadVerts;
			}
			if(quadColors!=NULL)
			{
				delete[] quadColors;
			}
			quadVerts=new float[(reader->W*reader->H)*3];
			quadColors=new float[(reader->W*reader->H)*3];
			idx=0;
			for(int h=0; h<reader->H; h++)
			{
				for(int w=0; w<reader->W; w++)
				{
					quadVerts[idx*3]=w+(647-reader->W)/2;
					quadVerts[idx*3+1]=h+(585-reader->H)/2;
					quadVerts[idx*3+2]=0.0;
					quadColors[idx*3]=((float)(reader->mVoxels[idx+(curFrame-1)*(reader->W*reader->H)]))/255.0f;
					quadColors[idx*3+1]=((float)(reader->mVoxels[idx+(curFrame-1)*(reader->W*reader->H)]))/255.0f;
					quadColors[idx*3+2]=((float)(reader->mVoxels[idx+(curFrame-1)*(reader->W*reader->H)]))/255.0f;

					idx++;
				}
			}
			glEnableClientState(GL_VERTEX_ARRAY);
			glEnableClientState(GL_COLOR_ARRAY);
			glVertexPointer(3, GL_FLOAT, 0, quadVerts);
			glColorPointer(3, GL_FLOAT, 0, quadColors);
			glDrawArrays(GL_POINTS, 0, (reader->W*reader->H));
			glDisableClientState(GL_VERTEX_ARRAY);
			glDisableClientState(GL_COLOR_ARRAY);
			glEnable(GL_LIGHTING);
		}

	}
	glFlush();
	return TRUE; // Keep Going

}

int LeoGraphics::getOneSlice(int frames)
{
	return 1;
}
